﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DNASequencing.assets
{
    class DNAcreator
    {
        public String sDNAsequence;
        public List<String> sSequences = new List<String>();
        public StreamWriter sw;
        public String filename;
        public int mistake;
        public bool type;
        public int repetition;
        public Random rand;

        /// <summary>
        /// Generate new dna file with 10 long oligonucleotides.
        /// </summary>
        /// <param name="file">Name of dna file</param>
        /// <param name="length">Length of generated DNA</param>
        /// <param name="mis">Number of errors</param>
        /// <param name="type">true -> positive, false -> negative</param>
        /// <param name="rep">Number of repetition of oligonucleotides</param>
        public DNAcreator(String file, int length, int mis, bool type, int rep)
        {
            filename = file;
            mistake = mis;
            this.type = type;
            repetition = rep;
            rand = new Random();
            sDNAsequence = makeSequence(length);
            makeOligonucleotides();
            makeErrors();
            makeRepetitions();
            sSequences.Sort();
            save();
        }

        public String makeSequence(int length)
        {
            string sequence = null;
            for (int i = 0; i < length; i++)
            {
                switch (rand.Next(4))
                {
                    case 0:
                        sequence += 'A';
                        break;
                    case 1:
                        sequence += 'C';
                        break;
                    case 2:
                        sequence += 'G';
                        break;
                    case 3:
                        sequence += 'T';
                        break;
                }
            }
            return sequence;
        }

        public void makeOligonucleotides()
        {
            for (int i = 0; i < sDNAsequence.Length - 10 + 1; i++)
            {
                sSequences.Add(sDNAsequence.Substring(i,10));
            }
        }

        public void makeErrors()
        {
            if (type)
            {
                String seq = null;
                for (int i = 0; i < mistake; i++)
                {
                    seq = makeSequence(10);
                    sSequences.Insert(rand.Next(sSequences.Count), seq);
                }
            }
            else
            {
                for (int i = 0; i < mistake; i++)
                {
                    sSequences.RemoveAt(rand.Next(sSequences.Count));
                }
            }
        }

        public void makeRepetitions()
        {
            String seq = null;
            for (int i = 0; i < repetition; i++)
            {
                seq = sSequences.ElementAt(rand.Next(sSequences.Count));
                sSequences.Add(seq);
            }
        }

        public void save()
        {
            using (sw = new StreamWriter(filename + ".dna"))
            {
                //sw.WriteLine(sDNAsequence);
                foreach (String seq in sSequences)
                {
                    sw.WriteLine(seq);
                }
            }
        }
    }
}
